/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package strategies;

import common.Application;
import java.util.Collection;
import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import parsers.ExtensionPanelParser;
import templates.ExtensionTemplate;
import templates.Template;

/**
 *
 * @author Alejandro
 */
public final class StrategySequence {

    private HttpServletRequest request;
    private boolean ignoreCustomStrategy = false;

    public StrategySequence(HttpServletRequest request, boolean ignoreCustomStrategy) {
        this.request = request;
        this.ignoreCustomStrategy = ignoreCustomStrategy;
    }

    public Iterable<Strategy> getSequence() {
        Collection<Strategy> sequence = new LinkedList<Strategy>();
        boolean save = request.getParameter("save") != null;
        boolean add = request.getParameter("add") != null;
        boolean edit = request.getParameter("edit") != null;
        boolean validate = request.getParameter("validate-add") != null || request.getParameter("validate-edit") != null;
        boolean delete = request.getParameter("delete") != null;
        boolean customStrategy = false;
        String custom = "";

        short tableSelection = (request.getParameter("table-selection") != null) ? Short.parseShort(request.getParameter("table-selection")) : 0;
        if (tableSelection != 0 && !ignoreCustomStrategy) {
            Template t = Application.getInstance().getTemplate(tableSelection);
            if (t.hasExtensions()) {
                Collection<String> params = new LinkedList<String>();
                for (ExtensionTemplate et: t.getExtensionTemplateCollection()) {
                    ExtensionPanelParser parser = new ExtensionPanelParser(et.getPanel().getText());
                    ExtensionPanelParser.State state = parser.run();
                    while (state != ExtensionPanelParser.State.Finished) {
                        if (state == ExtensionPanelParser.State.ControlName)
                            params.add(parser.getArgument());
                        state = parser.run();
                    }
                    
                    for (String s: params) {
                        if (request.getParameter(s) != null && !request.getParameter(s).isEmpty()) {
                            try {
                                Class c = Class.forName(et.getStrategy());
                                customStrategy = true;
                                custom = et.getStrategy();
                            } catch (ClassNotFoundException cfe) {
                            }
                            break;
                        }
                    }
                    params.clear();
                    if (customStrategy)
                        break;
                }
            }
        }

        //sequence.add(new RequestOutputStrategy());
        sequence.add(new JavascriptWarningStrategy());

        if (save && !validate) {
            sequence.add(new SaveEntityStrategy());
        }

        if (add || edit) {
            sequence.add(new EditEntityPageStrategy());
        }

        if (validate && save) {
            sequence.add(new ValidateEntityStrategy());
            sequence.add(new EditEntityPageStrategy());
        }

        if (delete) {
            sequence.add(new DeleteEntityStrategy());
        }

        if (!add && !edit && !validate)
            sequence.add(new ExtensionPanelStrategy(ignoreCustomStrategy));
        
        if (!add && !edit && !validate && !customStrategy) {
            sequence.add(new ShowEntityStrategy());
        }

        if (customStrategy) {
            sequence.add(StrategyFactory.createStrategy(custom));
        }

        return sequence;
    }
}
